package com.wt.hrentSystem.hrent.controller;

import com.wt.hrentSystem.common.config.ResponseCodeCanstants;
import com.wt.hrentSystem.common.entity.Page;
import com.wt.hrentSystem.common.entity.ResponseResult;
import com.wt.hrentSystem.common.entity.User;
import com.wt.hrentSystem.common.util.FileUpLoadUtils;
import com.wt.hrentSystem.common.util.UserUtils;
import com.wt.hrentSystem.hrent.entity.BikeResource;
import com.wt.hrentSystem.hrent.service.BikeResourceService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("bikeResource")
public class BikeResourceController {
    @Resource
    private BikeResourceService bikeResourceService;

    @Resource
    private UserUtils userUtils;

    /**
     * 根据id查询单条数据
     */
    @RequestMapping("get")
    public ResponseResult<BikeResource> get(String id){
        BikeResource bikeResource = bikeResourceService.get(id);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS, bikeResource,"成功");
    }

    /**
     *分页查询
     */
    @RequestMapping("findPage")
    public ResponseResult<Page> findPage(@RequestBody BikeResource bikeResource, HttpServletRequest request, HttpServletResponse response){
        bikeResource.setCurrentUser(userUtils.getUser(request));
        Page<BikeResource> page = new Page<>(bikeResource.getPageNo(), bikeResource.getPageSize());
        if(bikeResource.getTagList()!=null && bikeResource.getTagList().size()>0){
            bikeResource.setTagNum(bikeResource.getTagList().size());
        }
        Page<BikeResource> page1 = bikeResourceService.findPage(page, bikeResource);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,page1,"成功");
    }

    /**
     * 查询所有
     */
    @RequestMapping("findAll")
    public ResponseResult<List<BikeResource>> findAll(BikeResource bikeResource){
        List<BikeResource> list = bikeResourceService.findList(bikeResource);
        return new ResponseResult<>(ResponseCodeCanstants.SUCCESS,list,"成功");
    }

    /**
     * 增加或修改
     */
    @RequestMapping("save")
    public ResponseResult save(HttpServletRequest request,@RequestBody BikeResource bikeResource){
        bikeResource.setCurrentUser(userUtils.getUser(request));
        bikeResourceService.save(bikeResource);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,"成功");
    }

    /**
     * 删除
     */
    @RequestMapping("delete")
    public ResponseResult delete(BikeResource bikeResource, HttpServletRequest request){
        bikeResource.setCurrentUser(userUtils.getUser(request));
        bikeResourceService.delete(bikeResource);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,"成功");
    }

    /**
     * 上传图片
     */
    @RequestMapping("uploadPic")
    public ResponseResult uploadPic(HttpServletRequest request, MultipartFile file){
        String id = userUtils.getUser(request).getId();
        //图片地址
        String headUrl = "/img/"+id+"/bike/";
        FileUpLoadUtils fileUpLoadUtils = new FileUpLoadUtils();
        String visitPath = fileUpLoadUtils.uploadFile(file, headUrl);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,visitPath,"成功");
    }

    /**
     * 根据角色查询车辆信息
     */
    @RequestMapping("findByRole")
    public ResponseResult findByRole(HttpServletRequest request, @RequestBody BikeResource bikeResource, HttpServletResponse response){
        bikeResource.setCurrentUser(userUtils.getUser(request));
        Page<BikeResource> page = new Page<>(bikeResource.getPageNo(), bikeResource.getPageSize());
        if(bikeResource.getTagList()!=null && bikeResource.getTagList().size()>0){
            bikeResource.setTagNum(bikeResource.getTagList().size());
        }
        Page<BikeResource> page1 = bikeResourceService.findByRole(page, bikeResource);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,page1,"成功");
    }

    /**
     * 查询我看过的车辆
     */
    @RequestMapping("findViewedBikePage")
    public ResponseResult findViewedBikePage(HttpServletRequest request, @RequestBody BikeResource bikeResource, HttpServletResponse response){
        User user = userUtils.getUser(request);
        bikeResource.setCurrentUser(user);
        Page<BikeResource> page = new Page<>(bikeResource.getPageNo(), bikeResource.getPageSize());
        Page<BikeResource> page1 = bikeResourceService.findViewedBikePage(page, bikeResource);

        List<BikeResource> collect = page1.getList().stream().map(item -> {
            if (user != null) {
                BigDecimal latitude = user.getLatitude();
                BigDecimal longitude = user.getLongitude();
                item.setUserLatitude(latitude);
                item.setUserLongitude(longitude);
            }
            return item;
        }).collect(Collectors.toList());

        return new ResponseResult(ResponseCodeCanstants.SUCCESS,page1,"成功");
    }

    /**
     * 查询我收藏的车辆
     */
    @RequestMapping("findCollectionBikePage")
    public ResponseResult findCollectionBikePage(HttpServletRequest request, @RequestBody BikeResource bikeResource, HttpServletResponse response){
        User user = userUtils.getUser(request);
        bikeResource.setCurrentUser(user);
        Page<BikeResource> page = new Page<>(bikeResource.getPageNo(), bikeResource.getPageSize());
        Page<BikeResource> page1 = bikeResourceService.findCollectionBikePage(page, bikeResource);


        List<BikeResource> collect = page1.getList().stream().map(item -> {
            if (user != null) {
                BigDecimal latitude = user.getLatitude();
                BigDecimal longitude = user.getLongitude();
                item.setUserLatitude(latitude);
                item.setUserLongitude(longitude);
            }
            return item;
        }).collect(Collectors.toList());
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,page1,"成功");
    }

    /**
     * 查询我正在租的车辆
     */
    @RequestMapping("findRentingBikePage")
    public ResponseResult findRentingBikePage(HttpServletRequest request, @RequestBody BikeResource bikeResource, HttpServletResponse response){
        bikeResource.setCurrentUser(userUtils.getUser(request));
        Page<BikeResource> page = new Page<>(bikeResource.getPageNo(), bikeResource.getPageSize());
        Page<BikeResource> page1 = bikeResourceService.findRentingBikePage(page, bikeResource);
        return new ResponseResult(ResponseCodeCanstants.SUCCESS,page1,"成功");
    }
}
